var $j = jQuery;

var Diastole = (function(){
	var baseURL;
	
	return {
		init: function(homeURL){
			this.baseURL = homeURL;
			this.startSidebar();
			this.startAdminbar();
			
		},
		baseURL : function(){
			return this.baseURL;
		},
		decorate : function(links,targetDiv){
			var that = this;
			$j(links).each(function(){
				$j(this).click(function(){
					var href = that.getEntityId(this.href);
					that.loadSection(href,targetDiv);
				});
			});
		},
		decorateRequest : function(links,hrefaction,targetDiv){
			var that = this;
			$j(links).each(function(){
				$j(this).click(function(){
					var id = that.getEntityId(this.href);
					that.post(hrefaction,targetDiv,"id="+id);
				});
			});
		},
		getEntityId : function(href){
			//al hacer click modifique el anchor y que ahora figura en la url del browser
			var url = href.split("#");
			var entity = url[1]; //remuevo el numeral
			delete url;
			return entity;
		},
		loadScript: function(id,urlscript){
			var script = document.createElement("script");
			script.type = "text/javascript";
			script.id = id;
			script.src = this.baseURL + urlscript;
			document.body.appendChild(script);
		},
		loadSection: function(relativeURL, divSection){
			$j.get(this.baseURL + relativeURL,null,function(data){
				$j(divSection).html(data);
			});
		},
		post : function(relativeURL, divSection, params){
			$j.post(this.baseURL + relativeURL, params, function(data){
				$j(divSection).html(data);
			});
		},
		startSidebar : function(){
			this.decorate("div.main-navigation dt a.section","div.main-content"); //starting up menu links
			this.decorate("div.main-navigation dd a","div.main-content"); //starting up menu links
			
			$j("div.main-navigation dt a.subsection").each(function(){
				var menu = "#menu_" + $j(this).text().toLowerCase();
				$j(this).click(function(){
					var menuObj = $j(menu);
					if (menuObj.hasClass("inactive")){
						menuObj.removeClass("inactive");
						menuObj.addClass("active");
					}else{
						menuObj.removeClass("active");
						menuObj.addClass("inactive");
					}
					
				});
			});
		},
		startAdminbar : function(){
			this.decorate("div.nav2 ul li a.admin-section","div.main-content");
		}
	};
}()); 
$j(document).ready(function(){Diastole.init("http://diastole/diastole/");});